$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add('http://localhost:8080/')
$listener.Start()
Write-Host "服务器已启动: http://localhost:8080"

while ($listener.IsListening) {
    $context = $listener.GetContext()
    $request = $context.Request
    $response = $context.Response
    
    $path = $request.Url.LocalPath.TrimStart('/')
    if ($path -eq '') { $path = 'index.html' }
    
    $filePath = Join-Path (Get-Location) $path
    
    if (Test-Path $filePath -PathType Leaf) {
        $content = [System.IO.File]::ReadAllBytes($filePath)
        $response.ContentLength64 = $content.Length
        
        # 设置MIME类型
        $ext = [System.IO.Path]::GetExtension($filePath).ToLower()
        switch ($ext) {
            '.html' { $response.ContentType = 'text/html' }
            '.js' { $response.ContentType = 'application/javascript' }
            '.css' { $response.ContentType = 'text/css' }
            '.svg' { $response.ContentType = 'image/svg+xml' }
            default { $response.ContentType = 'application/octet-stream' }
        }
        
        $output = $response.OutputStream
        $output.Write($content, 0, $content.Length)
        $output.Close()
    } else {
        $response.StatusCode = 404
        $response.Close()
    }
}